package com.bighero.mq_to_mail.pAndC;

import com.bighero.mq_to_mail.pAndC.bean.WeatherBean;
import com.bighero.mq_to_mail.pAndC.utils.AssembleTianQi;
import com.bighero.mq_to_mail.pAndC.utils.Tianqi;
import com.bighero.mq_to_mail.pAndC.utils.Tool;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ProducerToMq {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    Tianqi tianqi;

    @Autowired
    AssembleTianQi assembleTianQi;

    @Scheduled(cron = "30 0 * * * *")
//    @PostConstruct
    public void scheduledProducerMsg(){
        //获取天气  海淀的编码
        WeatherBean weatherBean = tianqi.getTianqi("101010200");
        String tianqiString = assembleTianQi.zuZhuangTiqnQi(weatherBean);
        String queueName = "simple.queue";
        for (String s : Tool.ALL_MAIL.keySet()) {
            StringBuilder builder = new StringBuilder();
            builder.append("<html><body>");
            //写入内容
            builder.append("<p>尊敬的"+s+"您好：" + "</p>");
            builder.append("<p>    这里是北京市，海淀区，博彦大厦灾害预警中心，每日天气预报指挥部，下面为您播报今日天气：</p>");
            builder.append(tianqiString);
//        builder.append("<p>  本邮件为测试邮件，如有打扰烦请谅解问百度：<a href=\"https://www.baidu.com\">点击跳转到百度</a>。</p> ");
            builder.append("<p>  本邮件每天定时发送邮件，如有打扰烦请谅解，如需退订请致电：13781598321（同微）</p> ");
            //定义要输出日期字符串的格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //在内容后加入邮件发送的时间
            builder.append("<p> 发送时间：" + sdf.format(new Date()) + "</p>");
            builder.append("<p> 祝您今日愉快</p>");
            builder.append("</body></html>");
//            MailBean mailBean = new MailBean();
//            mailBean.setMsg();
//            mailBean.setName(s);
            rabbitTemplate.convertAndSend(queueName, builder.toString());
        }

    }
}
